<?php

namespace App\Http\Middleware;

use Cartalyst\Sentry\Facades\Laravel\Sentry;
use Closure;
use Illuminate\Support\Facades\Route;

class AdminAuth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $user = Sentry::getUser();
        $per = Route::currentRouteName();
        $groups = $user->getGroups();
        foreach($groups as $k=>$v){
            if($v['id']==1){
                return $next($request);
            }
        }
        try
        {
            // 要检查多个权限需要使用一个数组来进行参数传递
            if ($user->hasAccess($per))
            {
                return $next($request);
            }else {
				return response()->json(['status' => 300, 'msg' => '没有权限!']);
            }
        }
        catch (\Cartalyst\Sentry\Users\UserNotFoundException $e)
        {
			return response()->json(['status' => 300, 'msg' => '用户不存在!']);
        }
    }
}
